package com.aidong.chat.action;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.chat.biz.TrnSysInfoBiz;
import com.aidong.util.CalendarUtil;

/**
 * 系统消息action
 * @author user
 *
 */
@ParentPackage("json")
@Namespace("/")
public class TrnSysInfoAction {

	private TrnSysInfoBiz trnSysInfoBiz;

	@Autowired
	public void setTrnSysInfoBiz(TrnSysInfoBiz trnSysInfoBiz) {
		this.trnSysInfoBiz = trnSysInfoBiz;
	}
	
	Map<String, Object> map;

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	private int idongId;
	
	private int id;

	public void setIdongId(int idongId) {
		this.idongId = idongId;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	/**
	 * 我的系统消息列表
	 * @return
	 */
	@Action(value="gettrnsysinfo",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getTrnSysInfo(){
		map = new HashMap<String, Object>();
		try {
			if(idongId > 0){
				List<Map<String, Object>> infoMap = trnSysInfoBiz.getTrnSysInfo(idongId);
				if(infoMap.size() > 0){
					for (Map<String, Object> map : infoMap) {
						String dateTime = CalendarUtil.dealDate(map.get("senderDate").toString(), map.get("senderTime").toString());
						map.put("dateTime", dateTime);
						//map.remove("senderDate");
						//map.remove("senderTime");
					}
				}else{
					map.put("msg", 0);
					return "succ";
				}
				Collections.sort(infoMap, new Comparator<Map<String, Object>>() {
		            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
		            	return ((String) o2.get("senderDate")).compareTo((String)o1.get("senderDate"));
		            }
		        });
				map.put("code", 1); 
				map.put("result", infoMap);
				return "succ";
			}else{
				map.put("code", 0); 
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 切换消息未读状态
	 * @return
	 */
	@Action(value="editinfostatus",results={@Result(name="succ",type="json",params={"root","map"})})
	public String editSysInfoStatus(){
		map = new HashMap<String, Object>();
		
		try {
			if(id > 0 && idongId > 0){
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("idongId", idongId);
				paraMap.put("id", id);
				trnSysInfoBiz.editSysInfoStatus(paraMap);
				map.put("code", 1);
				return "succ";
			}else{
				map.put("code", 0); 
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}
